package com.ximu.user.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: java
 * @package: com.ximu.service_ucenter.entity
 * @className: LoginUser
 * @author: ximu
 * @description: null
 * @date: 2022/5/14 14:14
 * @version: 1.0
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    //permissions:权限
    private List<String> permissions;

    public LoginUser(User user) {
        this.user = user;

    }

    //存入Redis序列化时会把此成员变量给封装进去，需要进行一个忽略
    //提高作用域
    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities;

    //获取权限信息
//    @Override
//    public Collection<? extends GrantedAuthority> getAuthorities() {
//        if (authorities !=null){
//            return authorities;
//        }
//
//        //把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
////        for (String permission : permissions) {
////            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
////            authorities.add(simpleGrantedAuthority);
////        }
//        authorities = permissions.stream().
//                map(SimpleGrantedAuthority::new)
//                .collect(Collectors.toList());
//        return authorities;
//    }

    //security调用此方法，因此可以返回实体类的password
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
